package ua.edu.sumdu.j2se.roman.lab1.view.console;

import ua.edu.sumdu.j2se.roman.lab1.domain.Task;
import ua.edu.sumdu.j2se.roman.lab1.view.PostponeCommandView;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Author: Radko Roman.
 */

/**
 * This class responsible for creating view in case if need to postpone planned task.
 *
 */
public class ConsolePostponeCommandView implements PostponeCommandView{

    /**
     * Creates for read from the console date of the next alert.
     *
     */
    private Scanner scanner;
    /**
     * Creates for set format of the receiving date.
     *
     */
    private DateFormat format = new SimpleDateFormat(DATE_PATTERN);

    /**
     * Contains date format.
     *
     */
    public static final String DATE_PATTERN = "dd.MM.yy h:mm:ss";
    /**
     * Date of the next notification of postponed task.
     *
     */
    private Date nextTimeOfPostponeTaskNotification;

    public ConsolePostponeCommandView() {
        scanner = new Scanner(System.in);
    }

    /**
     * Gets next date notification of postponed task.
     *
     * @return next date
     */
    @Override
    public Date getNextDateOfPostponeTaskNotification() {
        return nextTimeOfPostponeTaskNotification;
    }
    /**
     * Ask user to enter date of the next notification of the task.
     *
     */
    @Override
    public void show(Task task) {
        System.out.println("Enter date of the next notification: ");
        nextTimeOfPostponeTaskNotification = read();
    }

    /**
     * Read date of the next task notification from console.
     *
     * @return  date of task
     */
    private Date read() {
        while (true) {
            System.out.print("       scheduled time in format " + DATE_PATTERN + " : ");
            String time = scanner.nextLine();
            try {
                Date result = format.parse(time);
                return result;
            } catch (ParseException e) {
                System.out.println("Check your date format, try again.");
            }
        }
    }

}
